//
// api.js
// vue3-element-admin
//
// Created by 区区电脑 on 2023/04/11.
//


import request from "@/request"


/**
 * 查询角色分页列表
 * @param params
 * @param data
 * @returns {*}
 * @constructor
 */
export function GetRolePageList(params = {}, data = {}) {
    return request({url: '/api/SysRole/GetRolePageList', method: 'POST', params, data})
}

/**
 * 查询角色列表
 * @param data
 * @returns {*}
 * @constructor
 */
export function GetRoleList(data = {}) {
    return request({url: '/api/SysRole/GetRoleList', method: 'POST', data})
}

/**
 * 添加角色
 * @param data
 * @returns {*}
 * @constructor
 */
export function AddRoleInfo(data = {}) {
    return request({url: '/api/SysRole/AddRoleInfo', method: 'POST', data})
}

/**
 * 修改角色
 * @param data
 * @returns {*}
 * @constructor
 */
export function UpdateRoleInfo(data = {}) {
    return request({url: '/api/SysRole/UpdateRoleInfo', method: 'PUT', data})
}

/**
 * 删除角色
 * @param params
 * @returns {*}
 * @constructor
 */
export function DeleteRoleInfo(params = {}) {
    return request({url: '/api/SysRole/DeleteRoleInfo', method: 'DELETE', params})
}

/**
 * 修改角色权限
 * @param data
 * @returns {*}
 * @constructor
 */
export function UpdateRolePermissions(data = {}) {
    return request({url: '/api/SysRole/UpdateRolePermissions', method: 'POST', data})
}

// 修改角色权限-新版
export function UpdateRoleDataPermissions(data = {}) {
    return request({url: '/api/SysRole/UpdateRoleDataPermissions', method: 'POST', data})
}

/**
 * 查询角色权限
 * @param params
 * @returns {*}
 * @constructor
 */
export function GetRolePermissionIdList(params = {}) {
    return request({url: '/api/SysRole/GetRolePermissionIdList', method: 'GET', params})
}

// 查询角色权限-新版
export function GetRolePermissionList(params = {}) {
    return request({url: '/api/SysRole/GetRolePermissionList', method: 'GET', params})
}

// 查询菜单树
export function GetMenuTreeList(params = {}) {
    return request({url: `/api/SysMenu/GetMenuTreeList`, method: 'get', params})
}

// 查询全部菜单树和指定角色拥有的菜单权限id 列表
// 并把角色拥有的权限对应的菜单设置为 hasPerm = true
export function getRoleMenuPermission(params = {}) {
    return new Promise(async (resolve) => {
        const [treeRes, permRes] = await Promise.all([
            GetMenuTreeList(),
            GetRolePermissionList(params)
        ])
        const tree = treeRes.data || [];
        const perms = permRes.data;
        if (!treeRes.success || !permRes.success) return resolve({success: false})
        tree.treeMap(item => {
            // 默认的数据权限值，后期根据情况更改
            if (item.type === 1) {
                item.dataScopeType = item.isLockDataPermission === 1 ? item.dataPermission : 2;
            }
            const perm = perms.find(perm => perm.sysMenuId === item.id)
            if (perm) {
                item.hasPerm = true;
                item.dataScopeType = perm.dataScopeType;
            }
        }, 'children')
        resolve({success: true, data: tree})
    })
}

export function GetOrgTreeList(params = {}) {
    return request({url: `/api/SysOrg/GetOrgTree`, method: 'get', params})
}
// 查询用户
export function GetSelectUserList(data = {}) {
    return request({url: "/api/SysUser/GetSelectUserList", method: "post", data})
}
// 选择用户
export function UpdateRoleUserList(data = {}) {
    return request({url: '/api/SysUser/UpdateRoleUserList', method: "put", data})
}

export function GetUserList(data = {}) {
    return request({url: '/api/SysUser/GetUserList', method: 'POST', data})
}
